package com.example.demo_text;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class VideoDAO {

    public static List<Video> getVideosByCourseId(String courseId) {
        List<Video> videos = new ArrayList<>();
        String sql = "SELECT * FROM video WHERE course_id = ?";

        try (Connection conn = UserDAO.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, courseId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                Video video = new Video();
                video.setVideoId(rs.getInt("video_id"));
                video.setCourseId(rs.getString("course_id"));
                video.setChapterTitle(rs.getString("chapter_title"));
                video.setVideoUrl(rs.getString("video_url"));
                video.setUploadTime(rs.getTimestamp("upload_time"));
                videos.add(video);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return videos;
    }

    public static void addVideo(String courseId, String chapterTitle, String videoUrl) {
        String sql = "INSERT INTO video (course_id, chapter_title, video_url) VALUES (?, ?, ?)";
        try (Connection conn = UserDAO.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, courseId);
            stmt.setString(2, chapterTitle);
            stmt.setString(3, videoUrl);
            stmt.executeUpdate();

        } catch (SQLException | IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
